/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean;

import java.io.IOException;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.event.ActionEvent;
import javax.faces.bean.ViewScoped;
import javax.faces.bean.ManagedBean;
import session.stateless.StakeholderSessionBean;
import javax.faces.context.FacesContext;
import session.stateless.SystemMessagesLocal;
import util.localization.WebLocalization;

/**
 *
 * @author jamie
 */
@ManagedBean
@ViewScoped //SessionScope
public class StakeholderLoginManagedBean implements Serializable {

    @EJB
    private StakeholderSessionBean stakeholderAccountMgt;
    // EXCEPTION
    @EJB
    private SystemMessagesLocal systemMessages;
    private WebLocalization localization;

    //  user preferred locale
    private String preferredLocale;
    private String username;
    private String password;
    private boolean loggedIn = false;
    private String nextPage;
    //for remembering me
    private boolean remember;
    private String r;

    public StakeholderLoginManagedBean() {
        //checkCookie();
    }

/*

    public void doLogin(ActionEvent event) throws UnsupportedEncodingException, NoSuchAlgorithmException {

        try {

            System.out.println("typed userName: " + username);

             System.out.println("typed userName: " + username);

            StakeholderAccountState stakeholderState = getStakeholderAccountMgt().getStakeholderByUsername(username);

            System.out.println("typed userNames: " + username);
            System.out.println(password);
            if (stakeholderState == null) {
                // before login

                String statusMessage = getSystemMessages().getSystemMessage("STAKEHOLDERACCOUNT_ACCT_DOES_NOT_EXIST", getPreferredLocale());

                 String statusMessage = getSystemMessages().getSystemMessage("STAKEHOLDERACCOUNT_ACCT_DOES_NOT_EXIST", getPreferredLocale());

                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, statusMessage, ""));
            } else {
                // after login

                if (stakeholderState.getPassword().equals(password)) {
                    String statusMessage = "LOGIN_SUCCESS";
                    if (stakeholderState.isVerified()) {
                        System.out.println("activation code: " + stakeholderState.isVerified());
                        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, statusMessage, ""));
                        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("stakeholderAccountState", stakeholderState);
                        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("stakeholderUsername", stakeholderState.getUsername());
                        loggedIn = true;
                        // send stakeholder account ID to next page through http request
                        //FacesContext.getCurrentInstance().getExternalContext().redirect("stakeholder/AddStakeholderPortalInfo.xhtml");// +stakeholderState.getId());
                        FacesContext ctx = FacesContext.getCurrentInstance();
                        nextPage = ctx.getExternalContext().encodeActionURL(ctx.getApplication().getViewHandler().getActionURL(ctx, "/stakeholder/AddStakeholderPortalInfo.xhtml"));

                        try {

                            ctx.getExternalContext().redirect(nextPage);
                        } catch (IOException ioe) {
                            ioe.printStackTrace();

                        }



                    }//end of stakeholder is verified;
                    else {
                        statusMessage = "Please check with t-cube on your account activation.";
                        System.out.println("activation code: " + stakeholderState.isVerified());
                        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, statusMessage, ""));
                    }
                } else {
                    String statusMessage = "WRONG PASSWORD or WRONG USERNAME";
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, statusMessage, ""));

                }
            }

        } catch (ExistException ex) {
            // EXCEPTION

            String statusMessage = getSystemMessages().getSystemMessage("LOGIN_FAIL", getPreferredLocale());
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, statusMessage, ""));


             String statusMessage = getSystemMessages().getSystemMessage("LOGIN_FAIL", getPreferredLocale());
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, statusMessage, ""));
        
        }
        //catch(IOException ex){
        //   String statusMessage = "REDIRECT_FAIL";
        //            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, statusMessage, ""));
        // }*/
    //}


    public void redirectPage(ActionEvent event) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        FacesContext ctx = FacesContext.getCurrentInstance();
        nextPage = ctx.getExternalContext().encodeActionURL(ctx.getApplication().getViewHandler().getActionURL(ctx, "/StakeholderRegister.xhtml"));

        try {

            ctx.getExternalContext().redirect(nextPage);
        } catch (IOException ioe) {
            ioe.printStackTrace();

        }

    }

    public void retrieveStakeholderInfo() {
        //this.setSelectedStakeholder(stakeholderSessionBean.retrieveStakeholderInfoByUsername(stakeholder.getUsername()));
    }

    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the loggedIn
     */
    public boolean isLoggedIn() {
        return loggedIn;
    }

    /**
     * @param loggedIn the loggedIn to set
     */
    public void setLoggedIn(boolean success) {
        this.loggedIn = success;
    }

    /**
     * @return the remember
     */
    public boolean isRemember() {
        return remember;
    }

    /**
     * @param remember the remember to set
     */
    public void setRemember(boolean remember) {
        this.remember = remember;
    }

    /**
     * @return the r
     */
    public String getR() {
        return r;
    }

    /**
     * @param r the r to set
     */
    public void setR(String r) {
        this.r = r;
    }

    /**
     * @return the nextPage
     */
    public String getNextPage() {
        return "StakeholderRegister?faces-redirect=true";
    }

    /**
     * @param nextPage the nextPage to set
     */
    public void setNextPage(String nextPage) {
        this.nextPage = nextPage;
    }

    /**
     * @return the stakeholderAccountMgt
     */
    public StakeholderSessionBean getStakeholderAccountMgt() {
        return stakeholderAccountMgt;
    }

    /**
     * @param stakeholderAccountMgt the stakeholderAccountMgt to set
     */
    public void setStakeholderAccountMgt(StakeholderSessionBean stakeholderAccountMgt) {
        this.stakeholderAccountMgt = stakeholderAccountMgt;
    }

    /**
     * @return the systemMessages
     */
    public SystemMessagesLocal getSystemMessages() {
        return systemMessages;
    }

    /**
     * @param systemMessages the systemMessages to set
     */
    public void setSystemMessages(SystemMessagesLocal systemMessages) {
        this.systemMessages = systemMessages;
    }

    /**
     * @return the localization
     */
    public WebLocalization getLocalization() {
        return localization;
    }

    /**
     * @param localization the localization to set
     */
    public void setLocalization(WebLocalization localization) {
        this.localization = localization;
    }

    /**
     * @return the preferredLocale
     */
    public String getPreferredLocale() {
        return preferredLocale;
    }

    /**
     * @param preferredLocale the preferredLocale to set
     */
    public void setPreferredLocale(String preferredLocale) {
        this.preferredLocale = preferredLocale;
    }
    public void logout(ActionEvent event)
    {
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("stakeholderAccountState", null);
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("StakeholderLogin.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(StakeholderLoginManagedBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

